package geo.com.geosdkdemo.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.device.devicebean.DeviceInfo;
import geo.com.geosdk.listener.GeoLocationDataInterface;
import geo.com.geosdkdemo.event.LocationUpdateEvent;
import geo.com.geosdkdemo.event.PowerBatteryLevelEvent;
import geo.com.geosdkdemo.util.GeneralUtil;
import geo.com.geosdkdemo.R;

public class SatelliteLocateInformationActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private TextView tvSatelliteNum;
    private TextView tvSolutionState;
    private TextView tvDelayTime;
    private TextView tvLongitude;
    private TextView tvLatitude;
    private TextView tvAltitude;
    private TextView tvHrms;
    private TextView tvVrms;
    private TextView tvPdop;
    private TextView tvHdop;
    private TextView tvVdop;
    private TextView tvTime;
    private TextView tvBatteryLevel;
    private String strHour;
    private String strMinu;
    private String strSec;
    private DeviceInfo deviceInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_satellite_lacate_information);
        initView();
        initListener();
        initData();
    }

    //电池电量更新
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showBatteryLevel(PowerBatteryLevelEvent event) {
        //电池电量
        tvBatteryLevel.setText(deviceInfo.mTagDeviceBasicInfo.mDeviceBatteryLevel);
    }

    //定位信息更新
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showUpdateLocationResultStatus(LocationUpdateEvent event) {
        showReceiveLocationData(event.getGeoLocationDataInterface());
    }

    private void showReceiveLocationData(GeoLocationDataInterface geoLocationData) {
        int totalSatelites = (int) geoLocationData.getSateInfoList().getSateInfoList().size();
        int userSatelites = geoLocationData.getSateInfoList().getGpsInLock() + geoLocationData.getSateInfoList().getGlonassInLock()
                + geoLocationData.getSateInfoList().getGaileoInLock() + geoLocationData.getSateInfoList().getBdInLock();
        tvSatelliteNum.setText(getString(R.string.satellite_num, userSatelites,totalSatelites));
        // 质量因子——0=未定位，1=GPS单点定位固定解，2=差分定位，3=PPS解；4=RTK固定解；5=RTK浮点解；6=估计值；
        // 7=手工输入模式；8=模拟模式；
        String name = "";
        switch (geoLocationData.getSolutionType().swigValue()){
            case -1:
                name=getString(R.string.text_quality_indicator_no_data);
                break;
            case 0:
                name=getString(R.string.text_quality_indicator_no_location);
                break;
            case 1:
                name=getString(R.string.text_quality_indicator_single);
                break;//Differential positioning
            case 2:
                name=getString(R.string.text_quality_indicator_diff_posi);
                break;
            case 3:
                name=getString(R.string.text_quality_indicator_pps);
                break;
            case 4:
                name=getString(R.string.text_quality_indicator_rtk_fix);
                break;
            case 5:
                name=getString(R.string.text_quality_indicator_rtk_float);
                break;
            case 6:
                name=getString(R.string.text_quality_indicator_rtk_estimate);
                break;
            case 7:
                name=getString(R.string.text_quality_indicator_rtk_input);
                break;
            case 8:
                name=getString(R.string.text_quality_indicator_simulation);
                break;

        }
        tvSolutionState.setText(name);
        tvDelayTime.setText(geoLocationData.getAgeOfDiff()+"s");
        tvLongitude.setText(GeneralUtil.dblToLocation(geoLocationData.getLongitude()));
        tvLatitude.setText(GeneralUtil.dblToLocation(geoLocationData.getLatitude()));
        tvAltitude.setText(String.valueOf(GeneralUtil.retain3Point((float)geoLocationData.getAltitude())));
        //VRMS,HRMS
        tvVrms.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getVrms())));
        tvHrms.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getHrms())));
        //pdop,hdop,vdop
        tvPdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getPdop())));
        tvHdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getHdop())));
        tvVdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getVdop())));
        if(geoLocationData.getLacalTime().getHour()<10 ){
            strHour=getString(R.string.time_3,geoLocationData.getLacalTime().getHour());
        }else{
            strHour= String.valueOf(geoLocationData.getLacalTime().getHour());
        }
        if(geoLocationData.getLacalTime().getMinute()<10 ){
            strMinu=getString(R.string.time_3,geoLocationData.getLacalTime().getMinute());
        }else{
            strMinu= String.valueOf(geoLocationData.getLacalTime().getMinute());
        }
        if(geoLocationData.getLacalTime().getSecond()<10 ){
            strSec=getString(R.string.time_3,geoLocationData.getLacalTime().getSecond());
        }else{
            strSec= String.valueOf(geoLocationData.getLacalTime().getSecond());
        }
        tvTime.setText(getString(R.string.local_time,geoLocationData.getLacalTime().getYear(),geoLocationData.getLacalTime().getMonth(),geoLocationData.getLacalTime().getDay(),strHour,strMinu,strSec));
    }

    protected void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        tvSatelliteNum = (TextView)findViewById( R.id.tv_satellite_num );
        tvSolutionState = (TextView)findViewById( R.id.tv_mcu_firmware_version);
        tvDelayTime = (TextView)findViewById( R.id.tv_delay_time);
        tvLongitude = (TextView)findViewById( R.id.tv_longitude );
        tvLatitude = (TextView)findViewById( R.id.tv_latitude );
        tvAltitude = (TextView)findViewById( R.id.tv_altitude );
        tvHrms = (TextView)findViewById( R.id.tv_hrms );
        tvVrms = (TextView)findViewById( R.id.tv_vrms );
        tvPdop = (TextView)findViewById( R.id.tv_pdop );
        tvHdop = (TextView)findViewById( R.id.tv_hdop );
        tvVdop = (TextView)findViewById( R.id.tv_vdop );
        tvTime = (TextView)findViewById( R.id.tv_time );
        tvBatteryLevel = (TextView)findViewById( R.id.tv_battery_level );

    }


    protected void initListener() {
        ivCommonBack.setOnClickListener(this);
    }


    protected void initData() {
        tvCommonTitle.setText("定位信息");
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        deviceInfo = GeoManage.getInstance().getDeviceInfo();
        tvBatteryLevel.setText(deviceInfo.mTagDeviceBasicInfo.mDeviceBatteryLevel);

    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
}
